<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 图片存储表
 */
class Goods_order_mdl extends CI_Model {

    const TABLE = 'goods_order';

    //添加数据
    public function create($data){
        return $this->db->insert(self::TABLE,$data);
    }

    public function select(){
        return $this->db->get(self::TABLE)->result_array();
    }

    public function img_count(){
        return $this->db->count_all_results(self::TABLE);
    }
    // 查询单个数据
    public function get_one(){
        return $this->db->get(self::TABLE)->row_array();
    }
    //删除数据
    public function delete(){
        return $this->db->delete(self::TABLE);
    }
    //修改数据
    public function update($data){
        return $this->db->update(self::TABLE,$data);
    }
    
    /**
     * 查询订单商品并组装
     * @param int $oid 订单表ID
     */
    public function get_goods($oid){
    	$this->db->flush_cache();
    	$this->db->where('order_id',$oid);
    	$list = $this->select();
    	$this->load->model('goods_mdl');
    	$this->load->model('goodsval_mdl');
    	
    	foreach($list as $k => $v){
    		
    		$this->db->where('id',$v['goods_id']);
    		$this->db->select('id,sort_id,name,brand,packaging');
    		$tmp_goods = $this->goods_mdl->select();
    		$tmp_goods = $this->goods_mdl->restr($tmp_goods);
    		$list[$k]['goods'] = $tmp_goods[0];
    		
    		//查询对应商品属性
    		$attribute = json_decode($v['attribute']);
    		$this->db->or_where_in('id',$attribute);
    		$res_val = $this->goodsval_mdl->select();
    		$list[$k]['goods_value'] = $this->goodsval_mdl->assembly($res_val);
    	}
    	return $list;
    }

}